package com.mdlib.droid.util.core;


import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.mdlib.droid.AppContext;
import com.mdlib.droid.R;


/**
 * Created by librabin on 16/9/12.
 */
public class ToastUtil {

    private static String lastToast = "";
    private static long lastToastTime;
    private static Toast mToast = null;


    public static void showToasts(String message) {
        showToasts(message, Toast.LENGTH_SHORT, Gravity.CENTER);
    }


    public static void showToasts(String message, int duration,
                                  int gravity) {
        if (message != null && !message.equalsIgnoreCase("")) {
            long time = System.currentTimeMillis();
            if (!message.equalsIgnoreCase(lastToast)
                    || Math.abs(time - lastToastTime) > 2000) {
                View view = LayoutInflater.from(AppContext.getInstance()).inflate(
                        R.layout.base_toasts, null);
                ((TextView) view.findViewById(R.id.tv_toast)).setText(message);
                mToast = new Toast(AppContext.getInstance());
                mToast.setView(view);
                mToast.setGravity(gravity, 0, 0);
                mToast.setDuration(duration);
                mToast.show();
                lastToast = message;
                lastToastTime = System.currentTimeMillis();
            }
        }
    }

    public static void hide() {
        if (mToast != null) {
            mToast.cancel();
        }
    }
}